using CollabApp.Domain.Entities.Game;

namespace CollabApp.Domain.Services.Game;

/// <summary>
/// 游戏状态管理服务接口
/// 负责管理游戏的整体状态，包括游戏生命周期、状态转换和状态验证
/// </summary>
public interface IGameStateService
{
    /// <summary>
    /// 初始化新游戏
    /// 创建游戏实例，设置初始状态，配置游戏参数
    /// </summary>
    /// <param name="gameId">游戏唯一标识</param>
    /// <param name="roomId">房间标识</param>
    /// <param name="gameSettings">游戏配置参数</param>
    /// <returns>初始化后的游戏实例</returns>
    Task<Entities.Game.Game> InitializeGameAsync(Guid gameId, Guid roomId, GameSettings gameSettings);

    /// <summary>
    /// 开始游戏
    /// 将游戏状态从等待转为进行中，启动游戏计时器
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <returns>操作是否成功</returns>
    Task<bool> StartGameAsync(Guid gameId);

    /// <summary>
    /// 结束游戏
    /// 终止游戏进程，计算最终结果，清理资源
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="reason">结束原因</param>
    /// <returns>游戏结束结果</returns>
    Task<GameEndResult> EndGameAsync(Guid gameId, GameEndReason reason);

    /// <summary>
    /// 获取游戏当前状态
    /// 返回游戏的实时状态信息
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <returns>游戏状态信息</returns>
    Task<GameStateInfo> GetGameStateAsync(Guid gameId);

    /// <summary>
    /// 验证状态转换的合法性
    /// 检查从当前状态到目标状态的转换是否被允许
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="targetState">目标状态</param>
    /// <returns>转换是否合法</returns>
    Task<bool> ValidateStateTransitionAsync(Guid gameId, Entities.Game.GameStatus targetState);

    /// <summary>
    /// 更新游戏状态
    /// 执行状态转换并触发相关事件
    /// </summary>
    /// <param name="gameId">游戏标识</param>
    /// <param name="newState">新状态</param>
    /// <param name="metadata">状态更新的元数据</param>
    /// <returns>更新是否成功</returns>
    Task<bool> UpdateGameStateAsync(Guid gameId, Entities.Game.GameStatus newState, Dictionary<string, object>? metadata = null);

    /// <summary>
    /// 创建团队游戏
    /// 初始化团队模式游戏，分配队伍，设置团队特殊规则
    /// </summary>
    /// <param name="roomId">房间ID</param>
    /// <param name="teamCount">队伍数量</param>
    /// <param name="playersPerTeam">每队人数</param>
    /// <param name="gameSettings">游戏配置</param>
    /// <returns>创建结果</returns>
    Task<CreateGameResult> CreateTeamGameAsync(Guid roomId, int teamCount, int playersPerTeam, GameSettings gameSettings);

    /// <summary>
    /// 创建生存游戏
    /// 初始化生存模式游戏，设置生存规则和死亡机制
    /// </summary>
    /// <param name="roomId">房间ID</param>
    /// <param name="gameSettings">游戏配置</param>
    /// <returns>创建结果</returns>
    Task<CreateGameResult> CreateSurvivalGameAsync(Guid roomId, GameSettings gameSettings);

    /// <summary>
    /// 创建极速游戏
    /// 初始化极速模式游戏，设置加速参数和快速节奏
    /// </summary>
    /// <param name="roomId">房间ID</param>
    /// <param name="speedMultiplier">速度倍数</param>
    /// <param name="gameSettings">游戏配置</param>
    /// <returns>创建结果</returns>
    Task<CreateGameResult> CreateSpeedGameAsync(Guid roomId, float speedMultiplier, GameSettings gameSettings);
}

/// <summary>
/// 游戏配置参数
/// </summary>
public class GameSettings
{
    public TimeSpan Duration { get; set; } = TimeSpan.FromMinutes(3);
    public int MaxPlayers { get; set; } = 6;
    public int MinPlayers { get; set; } = 2;
    public GameMode GameMode { get; set; } = GameMode.Classic;
    public int MapWidth { get; set; } = 1000;
    public int MapHeight { get; set; } = 1000;
    public string MapShape { get; set; } = "circle";
    public bool EnableDynamicBalance { get; set; } = true;
    public int PowerUpSpawnInterval { get; set; } = 25;
    public int MaxPowerUps { get; set; } = 3;
    public int SpecialEventChance { get; set; } = 0;
    public Dictionary<string, object> CustomSettings { get; set; } = new();
}

/// <summary>
/// 创建游戏结果
/// </summary>
public class CreateGameResult
{
    public bool Success { get; set; }
    public Guid? GameId { get; set; }
    public string Message { get; set; } = string.Empty;
    public List<string> Errors { get; set; } = new();
    public Dictionary<string, object> GameData { get; set; } = new();
}

/// <summary>
/// 游戏结束结果
/// </summary>
public class GameEndResult
{
    public Guid GameId { get; set; }
    public GameEndReason Reason { get; set; }
    public DateTime EndTime { get; set; }
    public List<PlayerGameResult> PlayerResults { get; set; } = new();
    public GameStatistics Statistics { get; set; } = new();
}

/// <summary>
/// 游戏状态信息
/// </summary>
public class GameStateInfo
{
    public Guid GameId { get; set; }
    public Entities.Game.GameStatus Status { get; set; }
    public DateTime StartTime { get; set; }
    public TimeSpan ElapsedTime { get; set; }
    public TimeSpan? RemainingTime { get; set; }
    public int ConnectedPlayers { get; set; }
    public Dictionary<string, object> StateData { get; set; } = new();
}

/// <summary>
/// 玩家游戏结果
/// </summary>
public class PlayerGameResult
{
    public Guid PlayerId { get; set; }
    public string PlayerName { get; set; } = string.Empty;
    public int Score { get; set; }
    public int Rank { get; set; }
    public TimeSpan PlayTime { get; set; }
    public Dictionary<string, object> Statistics { get; set; } = new();
}

/// <summary>
/// 游戏统计信息
/// </summary>
public class GameStatistics
{
    public TimeSpan TotalDuration { get; set; }
    public int TotalActions { get; set; }
    public int TotalPlayers { get; set; }
    public Dictionary<string, int> ActionCounts { get; set; } = new();
    public Dictionary<string, object> CustomStats { get; set; } = new();
}

/// <summary>
/// 游戏结束原因
/// </summary>
public enum GameEndReason
{
    Completed,      // 正常完成
    TimeExpired,    // 时间到
    PlayerLeft,     // 玩家离开
    ServerError,    // 服务器错误
    AdminTerminated // 管理员终止
}

/// <summary>
/// 游戏类型
/// </summary>
public enum GameType
{
    Territory,      // 领土争夺
    Survival,       // 生存模式
    Race,          // 竞速模式
    Puzzle         // 解谜模式
}

/// <summary>
/// 游戏模式枚举
/// </summary>
public enum GameMode
{
    /// <summary>
    /// 经典模式：标准规则，适合所有玩家
    /// </summary>
    Classic,
    
    /// <summary>
    /// 极速模式：移动速度+50%，90秒快速对战
    /// </summary>
    Speed,
    
    /// <summary>
    /// 道具狂欢：道具刷新频率×3，效果时间×1.5，特殊事件
    /// </summary>
    PowerUpCarnival,
    
    /// <summary>
    /// 生存模式：只有一条命，死亡即出局
    /// </summary>
    Survival,
    
    /// <summary>
    /// 团队模式：2v2或3v3，队友领地可以连通
    /// </summary>
    Team
}

/// <summary>
/// 难度等级
/// </summary>
public enum DifficultyLevel
{
    Easy,
    Normal,
    Hard,
    Expert
}
